home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Magazine / C_Tutorial / Part-8 / args1 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-04  |  3.2 KB  |  147 lines

  1. #include<dos/rdargs.h>
  2. #include<exec/libraries.h>
  3.  
  4. #include<stdio.h>
  5.  
  6. #include<clib/dos_protos.h>
  7. #include<clib/exec_protos.h>
  8.  
  9. #include "gui.h"
  10. #include "idcmp.h"
  11. #include "loadsave.h"
  12. #include "arexx.h"
  13.  
  14. /* The library base global variables */
  15. /* (The different style of opening libraries requires these to be initialised to NULL) */
  16. struct Library* GfxBase = NULL;
  17. struct Library* IntuitionBase = NULL;
  18. struct Library* GadToolsBase = NULL;
  19. struct Library* AslBase = NULL;
  20. struct Library* DosBase = NULL;
  21. struct Library* IFFBase = NULL;
  22. struct Library* RexxSysBase = NULL;
  23.  
  24. /* Need to give prototypes for our functions */
  25. int  createAll(void);
  26. void freeAll(void);
  27. int  openLibs(void);
  28. void closeLibs(void);
  29.  
  30. #define ARGS_TEMPLATE "DEPTH/N,PORTNAME"
  31.  
  32. enum ARGS { ARG_DEPTH, ARG_PORTNAME, NUM_ARGS };
  33.  
  34. #define DEFAULT_PORTNAME "HELLOPAINTER"
  35. #define DEFAULT_DEPTH    (4)
  36.  
  37. /* The start of the program */
  38. void main()
  39. {
  40.     if(createAll())
  41.         handleIDCMP();
  42.     freeAll();
  43. }
  44.  
  45. static struct RDArgs* rdargs = NULL;
  46.  
  47. int createAll()
  48. {
  49.   LONG args[NUM_ARGS];
  50.   int i;
  51.   /* Initialise our args to NULL */
  52.   /* (This way we will know if an argument was specified) */
  53.   for(i=0; i<NUM_ARGS; i++)
  54.       args[i] = NULL;
  55.   if(openLibs())
  56.     {
  57.       if(rdargs = ReadArgs(ARGS_TEMPLATE, args, NULL))
  58.         {
  59.             char* portname = (char*)(args[ARG_PORTNAME]);
  60.         LONG* depthptr = (LONG*)(args[ARG_DEPTH]);
  61.         UBYTE depth;
  62.             /* Use the default if an argument was not specified */
  63.         if(portname == NULL)
  64.             portname = DEFAULT_PORTNAME;
  65.         if(depthptr == NULL)
  66.             depth = DEFAULT_DEPTH;
  67.         else
  68.             depth = (UBYTE)(*depthptr);
  69.             return createARexxPort(portname) && createArgs() && openGUI(depth,0,0,0);
  70.         }
  71.         else
  72.             printf("Error: could not read arguments\n");
  73.     }
  74.     return FALSE;
  75. }
  76.  
  77. void freeAll()
  78. {
  79.     freeReqs();
  80.     closeGUI();
  81.     freeArgs();
  82.     freeARexxPort();
  83.     if(rdargs)
  84.         FreeArgs(rdargs);
  85.     closeLibs();
  86. }
  87.  
  88. /* Try to open all the libraries -- return TRUE on success */
  89. int openLibs()
  90. {
  91.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  92.     {
  93.         printf("Error: could not open graphics.library\n");
  94.         return FALSE;
  95.     }
  96.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  97.     {
  98.         printf("Error: could not open intuition.library\n");
  99.         return FALSE;
  100.     }
  101.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  102.     {
  103.         printf("Error: could not open gadtools.library\n");
  104.         return FALSE;
  105.     }
  106.     if((AslBase = OpenLibrary("asl.library",37)) == NULL)
  107.     {
  108.         printf("Error: could not open asl.library\n");
  109.         return FALSE;
  110.     }
  111.     if((DosBase = OpenLibrary("dos.library",37)) == NULL)
  112.     {
  113.         printf("Error: could not open dos.library\n");
  114.         return FALSE;
  115.     }
  116.     if((IFFBase = OpenLibrary("iff.library",23)) == NULL)
  117.     {
  118.         printf("Error: could not open iff.library\n");
  119.         return FALSE;
  120.     }
  121.     if((RexxSysBase = OpenLibrary("rexxsyslib.library",35)) == NULL)
  122.     {
  123.         printf("Error: could not open rexxsyslib.library\n");
  124.         return FALSE;
  125.     }
  126.   return TRUE;
  127. }
  128.  
  129. /* Close any open library */
  130. void closeLibs()
  131. {
  132.     if(RexxSysBase)
  133.         CloseLibrary(RexxSysBase);
  134.     if(IFFBase)
  135.         CloseLibrary(IFFBase);
  136.     if(DosBase)
  137.         CloseLibrary(DosBase);
  138.     if(AslBase)
  139.         CloseLibrary(AslBase);
  140.     if(GadToolsBase)
  141.         CloseLibrary(GadToolsBase);
  142.     if(IntuitionBase)
  143.         CloseLibrary(IntuitionBase);
  144.     if(GfxBase)
  145.         CloseLibrary(GfxBase);
  146. }
  147.